<?php 
class SqlHelper{
    private $conn;
    private $username='ems';
    private $password='ems@123';
    private $db='ems';
    private $host='127.0.0.1';
    
    public function __construct(){
        
        $this->conn=new mysqli($this->host,$this->username,$this->password,$this->db);
        $this->conn->query("set names utf8");
        if(!$this->conn){
            die(mysqli_connect_error());
        }
      
    }
   
    public function execute_dql($sql){
        $res=$this->conn->query($sql) or die($this->conn->error());
        return $res;
    }
    //$sql1="select * from emp limit ?,?";
    //$sql2="select count(id) from emp ";
    public function execute_dql_fenye($sql1,$sql2,$fenyePage){
        $res=$this->conn->query($sql1);
        $arr=array();
        while($row=mysqli_fetch_array($res)){
            $arr[]=$row;
        }
       
        mysqli_free_result($res);
        $res=$this->conn->query($sql2);
        if($row=mysqli_fetch_row($res)){
            $fenyePage->pageCount=ceil($row[0]/$fenyePage->pageSize);
            $fenyePage->rowCount=$row[0];
        }
        $fenyePage->res_array=$arr;
        //显示上一页 下一页
        if($fenyePage->pageNow>1){
            $prePage=$fenyePage->pageNow-1;
            $navigate="<a href={$fenyePage->gotoUrl}?pageNow=$prePage>上一页</a> &nbsp;";
        }
        if($fenyePage->pageNow<$fenyePage->pageCount){
            $nextPage=$fenyePage->pageNow+1;
            $navigate.= "<a href={$fenyePage->gotoUrl}?pageNow=$nextPage>下一页</a> &nbsp;";
        }
        
        $roll_page=10;
        $start=floor(($fenyePage->pageNow-1)/$roll_page)*$roll_page+1;
        $index=$start;
        
        if($fenyePage->pageNow>$roll_page){
            $navigate.="<a href='{$fenyePage->gotoUrl}?pageNow=".($start-1)."'> &nbsp; << </a>";
        }
        for(;$start<$index+$roll_page;$start++){
            $navigate.="<a href='{$fenyePage->gotoUrl}?pageNow=$start'>[$start]<a/>";
        }
        if($fenyePage->pageNow<$fenyePage->pageCout-10){
            $navigate.="<a href='{$fenyePage->gotoUrl}?pageNow=$start'> &nbsp; >> </a>";
        }
        $navigate.="当前{$fenyePage->pageNow}页数/共{$fenyePage->pageCount}页"; 
        $fenyePage->navigate=$navigate;
    }
    //
    public function execute_dql2($sql){
        $arr=array();
        //$i=0;
        $res=$this->conn->query($sql) ;//or die($this->conn->error());
        while($row=mysqli_fetch_array($res)){
            $arr[]=$row;
        }
        mysqli_free_result($res);
        return $arr;
    }
   
    public function execute_dml($sql){
        $b=$this->conn->query($sql);
        if(!$b){
            return 0;
        }else{
            if($this->conn->affected_rows>0){
                return 1;//表示执行成功
            }else{
                return 2;//表示没有行受到影响
            }
        }
    }
    public function close_sql(){
       $this->conn->close(); 
    }
}

?>